*************************************************************
*** RUN JACKKNIFE REGS FOR FALSIFICATION TEST             ***
*** Last edited: 7/18/2021         		   	    		  ***
*************************************************************
log using "${CodePath}/log/regs_hosp_100mradius_falsification_$S_DATE.log", text replace

cap mkdir "${OutputPath}/Falsification Border/"
cap mkdir "${OutputPath}/Falsification Border/jackknife_audit_in2011/"
cap mkdir "${OutputPath}/Falsification Border/jackknife_audit_in2011/"
cap mkdir "${OutputPath}/Falsification Border/jackknife_audit_in2011/hospcomp_100m/"
cap mkdir "${OutputPath}/Falsification Border/jackknife_audit_in2011/hospcomp_100m/Event Studies/"
cap mkdir "${OutputPath}/Falsification Border/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/"
cap mkdir "${OutputPath}/Falsification Border/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/"



*--------------------------------------------------
* Select and create dataset for regressions
*--------------------------------------------------

local hospvars    "log_n_claims"

// import mapping of hospitals to falsification borders -- available in replication packet
use "${DataPath}/falsification-borders/pnlist_minusRACborder_within100m_groups_100m.dta", clear
rename segm_id nearseg100_state_2
duplicates drop hospcomp_group_100 pn, force
expand 9
sort pn
bys pn hospcomp_group_100 : gen fyear = _n + 2006
// create a small dataset with relevant vars
preserve
	use "${DataPath}/derived/hospyear_0716_jk.dta", clear

	keep pn fyear dist_to_border jk_RACregion_audit_in2011* jk_state_audit_in2011* state_audit_in2011* audit_in2011* jk_RACregion_audit_in2011* demandrate_in2011* jk_RACregion_demand_in2011*  jk_state_demand_in2011* `hospvars' `heterovars' nearseg100_state_2 RACregion state
	keep if fyear >= 2007 & fyear <= 2015
	tempfile temp
	save `temp'
restore

merge m:1 pn fyear using `temp', keep(1 3) gen(_jkmerge)


egen hospcomp_group_fyear = group(hospcomp_group_100 fyear)

	
*--------------------------------------------------
* Make event studies
*-------------------------------------------------- 
foreach hospvar of varlist `hospvars' {
	
	// jackknife state audit rate as instrument
		ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
	 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
				,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
		iv_event_study95 audit_in2011
		graph export "${OutputPath}/Falsification Border/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV.png", replace  

} // hospvar


*--------------------------------------------------
* Define groups by within 100m and if they are along an interior MAC border
*--------------------------------------------------
  
use "${DataPath}/falsification-borders/pn_minusRACborder_within100m_distmatrix.dta", clear
keep if ingroup_100 == 1 | pn_1 == pn_2
sort pn_1 pn_2

  // want to only keep any interior borders that have a combination of at least 2 of these states
  // MAC border 1: Oregon (OR), Idaho (ID), Nevada (NV), Utah (UT), Arizona (AZ), California (CA)
  // MAC border 2: N Dakota (ND), S Dakota (SD), Minnesota (MN), Nebraska (NE), Iowa (IA)
  // MAC border 3: Indiana (IN), Illinois (IL), Kentucky (KY)
  // MAC border 4: South Carolina (SC), Georgia (GA), Tennessee (TN), North Carolina (NC), Virginia (VA)
  // MAC border 5: Alabama (AL), Georgia (GA), Florida (FL)
  // MAC border 6: Pennsylvania (PA), New York (NY)

gen mac_border1_1 = state_1 == "OR" | state_1 == "ID" | state_1 == "NV" | state_1 == "UT" | state_1 == "AZ" | state_1 == "CA"
gen mac_border2_1 = state_1 == "ND" | state_1 == "SD" | state_1 == "MN" | state_1 == "NE" | state_1 == "IA"
gen mac_border3_1 = state_1 == "IN" | state_1 == "IL" | state_1 == "KY" 
gen mac_border4_1 = state_1 == "SC" | state_1 == "GA" | state_1 == "TN" | state_1 == "NC" | state_1 == "VA"
gen mac_border5_1 = state_1 == "AL" | state_1 == "GA" | state_1 == "FL"
gen mac_border6_1 = state_1 == "PA" | state_1 == "NY"

gen mac_border1_2 = state_2 == "OR" | state_2 == "ID" | state_2 == "NV" | state_2 == "UT" | state_2 == "AZ" | state_2 == "CA"
gen mac_border2_2 = state_2 == "ND" | state_2 == "SD" | state_2 == "MN" | state_2 == "NE" | state_2 == "IA"
gen mac_border3_2 = state_2 == "IN" | state_2 == "IL" | state_2 == "KY" 
gen mac_border4_2 = state_2 == "SC" | state_2 == "GA" | state_2 == "TN" | state_2 == "NC" | state_2 == "VA"
gen mac_border5_2 = state_2 == "AL" | state_2 == "GA" | state_2 == "FL"
gen mac_border6_2 = state_2 == "PA" | state_2 == "NY"

gen any_matching_macborder = (mac_border1_1 == 1 & mac_border1_2 == 1) | (mac_border2_1 == 1 & mac_border2_2 == 1) | (mac_border3_1 == 1 & mac_border3_2 == 1) | (mac_border4_1 == 1 & mac_border4_2 == 1) | (mac_border5_1 == 1 & mac_border5_2 == 1) | (mac_border6_1 == 1 & mac_border6_2 == 1)  

keep if any_matching_macborder

// make a unique name for the hospital comparison group (by appending strings of pns in each hospital comparison)
gen strL hospcomp_group_100 = ""

levelsof pn_1, local(hosplist)
foreach hosp of local hosplist{
  di "`hosp'"
   qui levelsof pn_2 if pn_1 == `hosp', local(pn_2list)
   replace hospcomp_group_100 = "`pn_2list'" if pn_1 == `hosp'
}

bys pn_1: egen n_hospcomp_ingroup_100 = count(pn_2)

// a list of hospitals and the comparison groups (circles around hospital of radius 100 mile) they are a part of, repeated for the number of times they are in a group
keep pn_2 hospcomp_group_100 n_hospcomp_ingroup_100 segm_id_2 state_2 RACregion_2
rename pn_2 pn
rename segm_id_2 segm_id
rename state_2 state
rename RACregion_2 RACregion_
bys pn: egen n_group_partof_100 = count(hospcomp_group_100)
egen tag = tag(pn)

egen tag2 = tag(hospcomp_group_100)

egen tag3 = tag(hospcomp_group_100 RACregion)
egen n_RAC_ingroup_100 = total(tag3), by(hospcomp_group_100)

drop tag*

save "${DataPath}/falsification-borders/pnlist_MACborders_minusRACborder_within100m_groups_100m.dta", replace

*--------------------------------------------------
* All interior MAC borders
*--------------------------------------------------

use "${DataPath}/Falsification Borders/pnlist_MACborders_minusRACborder_within100m_groups_100m.dta", clear
rename segm_id nearseg100_state_2
duplicates drop hospcomp_group_100 pn, force
expand 9
sort pn
bys pn hospcomp_group_100 : gen fyear = _n + 2006
// create a small dataset with relevant vars
preserve
	use "${DataPath}/derived/hospyear_0716_jk.dta", clear
	bys RACregion: egen temp = mean(demandafterauditrate_in2011) if fyear == 2011
	bys pn (fyear): egen RACavg_demandafteraudit_in2011 = mean(temp)
	drop temp
	bys state: egen temp = mean(demandafterauditrate_in2011) if fyear == 2011
	bys pn (fyear): egen stateavg_demandafteraudit_in2011 = mean(temp)
	drop temp

	keep pn fyear dist_to_border jk_RACregion_audit_in2011* jk_state_audit_in2011* state_audit_in2011* audit_in2011* jk_RACregion_audit_in2011* nearseg100_state_2 RACregion state log_n_claims
	keep if fyear >= 2007 & fyear <= 2015
	tempfile temp
	save `temp'
restore

merge m:1 pn fyear using `temp', keep(1 3) gen(_jkmerge)


egen hospcomp_group_fyear = group(hospcomp_group_100 fyear)

ivreghdfe  log_n_claims (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
			,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
iv_event_study95 audit_in2011
graph export "${OutputPath}/Falsification Border/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/macborders_log_n_claims_IV.pdf", replace



log close

